//
//  ClassViewController.m
//  ChunLaiZhuangClient
//
//  Created by zouzhisheng on 16/2/15.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "ClassViewController.h"
#import "CustomTableViewCellTwo.h"
#import "ClassDataModel.h"
#import "ClassFrameModel.h"
#import "CustomClassTableViewCell.h"
#import "SearchViewController.h"
#import "BrandFrameModel.h"
#import "CustomBrandTableViewCell.h"
#import "BrandHeadTableViewCell.h"
@interface ClassViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>{
    UIView *bottomLineView;
    UIButton *item1;
    UIButton *item2;
    UITableView *leftTableView;
    UITableView *rightTableView;
    NSMutableArray *framesArray;  //左边数据源
    NSMutableArray *rightFramesArray; //右边数据源
    ClassModel *leftModel;  //左边分类model
    BrandGroupModel *rightModel;  //右边品牌model
}
@property(nonatomic,strong)UIScrollView *bgScrollView;
@end

@implementation ClassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadLeftData];
    [self.view addSubview:self.searchView];
    [self.view addSubview:self.itemMenuView];
    [self.view addSubview:self.bgScrollView];
    
       // Do any additional setup after loading the view from its nib.
}
-(void)formatLeftDataModel{
    framesArray=[[NSMutableArray alloc] init];
    for (int i=0; i<leftModel.data.categories.count; i++) {
        CategoryModel *categoryModel=leftModel.data.categories[i];
        ClassDataModel *dataModel=[[ClassDataModel alloc] init];
        dataModel.imageViewUrl=categoryModel.image;
        dataModel.title=categoryModel.name;
        dataModel.CategoryID=categoryModel.category_id;
        dataModel.classArray=categoryModel.subcategories;
        ClassFrameModel *framModel=[[ClassFrameModel alloc] init];
        [framModel setDataModel:dataModel];
        [framesArray addObject:framModel];
        
    }
 
}
-(void)formatRightDataModel{
    rightFramesArray=[[NSMutableArray alloc] init];
    for (int i=0; i<rightModel.data.brand_groups.count; i++) {
        BrandGroupDetailModel *detailModel=rightModel.data.brand_groups[i];
        BrandDataModel *model=[[BrandDataModel alloc] init];
        model.title=detailModel.name;
        model.dataArray=detailModel.brands;
        BrandFrameModel *frmeModel=[[BrandFrameModel alloc] init];
        [frmeModel setModel:model];
        [rightFramesArray addObject:frmeModel];
    }

}
-(void)loadLeftData{
     [[LKDHud shareHud] showLoadingView];
    [ApiClient getAllCategoriesDidFinishLoaded:^(ClassModel *model, NSString *path) {
         [[LKDHud shareHud] hideLoadingView];
        leftModel = model;
        [self formatLeftDataModel];
        [leftTableView reloadData];
        [leftTableView.mj_header endRefreshing];
    } didFailLoaded:^(NSError *error, NSString *path) {
         [[LKDHud shareHud] hideLoadingView];
         [leftTableView.mj_header endRefreshing];
    }];
}
-(void)loadRightData{
    [ApiClient getBrandsGroupedDidFinishLoaded:^(BrandGroupModel *model, NSString *path) {
        rightModel=model;
        [self formatRightDataModel];
        [rightTableView reloadData];
        [rightTableView.mj_header endRefreshing];
    
    } didFailLoaded:^(NSError *error, NSString *path) {
        [rightTableView.mj_header endRefreshing];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(UIView*)searchView{
    if(!_searchView){
        _searchView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 122/2)];
        _searchView.backgroundColor=[UIColor colorWithHexString:@"fafafa"];
        UIButton *searchBt=[UIButton buttonWithType:UIButtonTypeCustom];
        searchBt.frame=CGRectMake(10, 27, SCREEN_WIDTH-20, 34);
        searchBt.layer.cornerRadius=18;
        searchBt.clipsToBounds=YES;
        searchBt.layer.borderColor=[UIColor colorWithHexString:@"d8d8f8"].CGColor;
        searchBt.layer.borderWidth=0.5;
        searchBt.backgroundColor=[UIColor whiteColor];
         [searchBt addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
        UIImageView *searchIcon=[[UIImageView alloc] initWithFrame:CGRectMake(searchBt.width/2-45-16, 9, 16, 16)];
        searchIcon.image=[UIImage imageNamed:@"icon_search"];
        
        [searchBt addSubview:searchIcon];
        UILabel *placeLael=[[UILabel alloc] initWithFrame:CGRectMake(0, 2, 200, 30)];
        placeLael.text=@"输入商品名或品牌名";
        placeLael.textColor=[UIColor colorWithHexString:@"c8c8c8"];
        placeLael.font=[UIFont systemFontOfSize:12];
        placeLael.left=searchIcon.right+7;
        [searchBt addSubview:placeLael];
        [_searchView addSubview:searchBt];
    }
    return _searchView;
}
-(UIView*)itemMenuView{
    if(!_itemMenuView){
        _itemMenuView=[[UIView alloc] initWithFrame:CGRectMake(0,_searchView.bottom,SCREEN_WIDTH, 40)];
        item1=[UIButton buttonWithType:UIButtonTypeCustom];
        item1.frame=CGRectMake(0, 10, 60,30);
        item1.right=SCREEN_WIDTH/2-34;
        [item1 setTitle:@"分类" forState:UIControlStateNormal];
        [item1 setTitleColor:[UIColor colorWithHexString:@"b0b0b0"] forState:UIControlStateNormal];
        [item1 setTitleColor:[UIColor colorWithHexString:@"f77878"] forState:UIControlStateSelected];
        item1.titleLabel.font=[UIFont systemFontOfSize:18];
        item1.tag=1;
        [item1 addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
        item1.selected=YES;
        item2=[UIButton buttonWithType:UIButtonTypeCustom];
        item2.frame=CGRectMake(0, 10, 60,30);
        item2.left=SCREEN_WIDTH/2+37;
        [item2 setTitle:@"品牌馆" forState:UIControlStateNormal];
        [item2 setTitleColor:[UIColor colorWithHexString:@"b0b0b0"] forState:UIControlStateNormal];
        [item2 setTitleColor:[UIColor colorWithHexString:@"f77878"] forState:UIControlStateSelected];
        item2.titleLabel.font=[UIFont systemFontOfSize:18];
        [item2 addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
        item2.tag=2;
        _itemMenuView.backgroundColor=[UIColor colorWithHexString:@"fafafa"];
        [_itemMenuView addSubview:item1];
        [_itemMenuView addSubview:item2];
        
        UIView *line1=[[UIView alloc] initWithFrame:CGRectMake(item1.right+34, 18,0.5,11)];
        line1.backgroundColor=[UIColor colorWithHexString:@"d8d8d8"];
        [_itemMenuView addSubview:line1];
        bottomLineView=[[UIView alloc] initWithFrame:CGRectMake(0, 0,44, 2)];
        bottomLineView.bottom=_itemMenuView.height;
        bottomLineView.backgroundColor=[UIColor colorWithHexString:@"f77171"];
        bottomLineView.center=CGPointMake(item1.center.x, bottomLineView.center.y);
        [_itemMenuView addSubview:bottomLineView];
        
        UIView *line2=[[UIView alloc] initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,0.5)];
        line2.backgroundColor=[UIColor colorWithHexString:@"d8d8d8"];
        line2.bottom=_itemMenuView.height;
        [_itemMenuView addSubview:line2];
        
    }
    return _itemMenuView;
}
-(UIScrollView*)bgScrollView{
    __weak typeof(self) weakSelf = self;
    if (!_bgScrollView) {
        _bgScrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0,self.itemMenuView.bottom, SCREEN_WIDTH,SCREEN_HEIGHT-self.itemMenuView.bottom-49)];
        _bgScrollView.delegate=self;
        _bgScrollView.contentSize=CGSizeMake(SCREEN_WIDTH*2, SCREEN_HEIGHT-self.itemMenuView.bottom-49);
        _bgScrollView.pagingEnabled=YES;
        leftTableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH, SCREEN_HEIGHT-self.itemMenuView.bottom-49) style:UITableViewStylePlain];
        leftTableView.dataSource=self;
        leftTableView.delegate=self;
        leftTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        [_bgScrollView addSubview:leftTableView];
        leftTableView.mj_header = [CLZRefreshHeader headerWithRefreshingBlock:^{
            [weakSelf loadLeftData];
        }];
        
        rightTableView=[[UITableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0,SCREEN_WIDTH, SCREEN_HEIGHT-self.itemMenuView.bottom-49) style:UITableViewStylePlain];
        rightTableView.dataSource=self;
        rightTableView.delegate=self;
        rightTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        rightTableView.tableFooterView=[[UIView alloc] initWithFrame:CGRectZero];
        BrandHeadTableViewCell* headView = [[[NSBundle mainBundle] loadNibNamed:@"BrandHeadTableViewCell"  owner:self options:nil] lastObject];
        rightTableView.tableHeaderView=headView ;
        rightTableView.backgroundColor=[UIColor clearColor];
        rightTableView.mj_header = [CLZRefreshHeader headerWithRefreshingBlock:^{
            [weakSelf loadRightData];
        }];
        [_bgScrollView addSubview:rightTableView];
        
    }
    return _bgScrollView;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
#pragma mark-buttonAciton
-(void)selectAction:(UIButton*)bt{
    if (bt==item1) {
        if (!item1.selected) {
            item1.selected=YES;
            item2.selected=NO;
        }
   
    }
    if (bt==item2) {
        if (!item2.selected) {
            item2.selected=YES;
            item1.selected=NO;
        }
    }
        [UIView animateWithDuration:0.2 animations:^{
        bottomLineView.center=CGPointMake(bt.center.x, bottomLineView.center.y);
    }];
    switch (bt.tag) {
        case 1:
            [_bgScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
            break;
        case 2:
             [_bgScrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:YES];
            break;
        default:
            break;
    }
}
#pragma mark-UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView==leftTableView) {
        return framesArray.count;
    }else{
        return 1;
    }
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (tableView==leftTableView) {
        return 1;
    }else{
        return rightModel.data.brand_groups.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==leftTableView) {
        CustomClassTableViewCell *cell=[CustomClassTableViewCell cellWithTableView:tableView];
        cell.frameModel=framesArray[indexPath.row];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        CustomBrandTableViewCell*cell=[CustomBrandTableViewCell cellWithTableView:tableView];
        cell.frameModel=rightFramesArray[indexPath.section];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
        
    }
   
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView==leftTableView) {
        ClassFrameModel *frameModel=framesArray[indexPath.row];
        return frameModel.cellHeight;
    }else{
        BrandFrameModel *frameModel=rightFramesArray[indexPath.section];
        return frameModel.cellHeight;
    }
   
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (tableView==rightTableView) {
        return 35;
    }else{
        return 0;
    }
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (tableView==rightTableView) {
        UIView *aview=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
        aview.backgroundColor=[UIColor colorWithHexString:@"efeff4"];
        UILabel *titleLable=[[UILabel alloc] initWithFrame:CGRectMake(12, 10, 100, 20)];
        titleLable.textColor=[UIColor blackColor];
        BrandFrameModel *frameModel=rightFramesArray[section];
        titleLable.text=frameModel.model.title;
        titleLable.font=[UIFont systemFontOfSize:14];
        [aview addSubview:titleLable];
        return aview;
    }else{
        return [[UIView alloc] initWithFrame:CGRectZero];
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (_bgScrollView==scrollView) {
        if (scrollView.contentOffset.x==0) {
            [UIView animateWithDuration:0.2 animations:^{
                bottomLineView.center=CGPointMake(item1.center.x, bottomLineView.center.y);
            }];
            
        }
        if (scrollView.contentOffset.x==SCREEN_WIDTH) {
            [UIView animateWithDuration:0.2 animations:^{
                bottomLineView.center=CGPointMake(item2.center.x, bottomLineView.center.y);
            }];
            if (!rightModel) {
               [self loadRightData];
            }
           
        }
        
    }

}
-(void)searchAction:(UIButton*)bt{
    SearchViewController *searchVC=[[SearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:NO];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
